<?php

namespace App\Http\Controllers\OpenShop\SiteLocation;

use App\Http\Controllers\BaseController;
use App\Service\SiteLocation\SiteTaskService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class SiteTaskController extends BaseController
{
    /**
     * 数据总览
     * @param Request $request
     * @return JsonResponse
     */
    public function overview(Request $request): JsonResponse
    {
        return $this->success(SiteTaskService::overview());
    }


    /**
     * 任务列表
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function list(Request $request): JsonResponse
    {
        $rmsUserId = getRmsUserId();

        // 筛选参数
        $provinceCode       = (int)$request->get('provinceCode', 0);
        $cityCode           = (int)$request->get('cityCode', 0);
        $districtCode       = (int)$request->get('districtCode', 0);
        $wgType             = $request->get('wgType', '');
        $wgName             = $request->get('wgName', '');
        $grade              = $request->get('grade', '');
        $ranking            = (int)$request->get('ranking', '');
        $settle             = $request->get('settle', '');
        $manageStatus       = $request->get('manageStatus', '');
        $projectPhase       = $request->get('projectPhase', '');
        $assignId           = (int)$request->get('assignId', 0);
        $assignStartTime    = $request->get('assignStartTime', '');
        $assignEndTime      = $request->get('assignEndTime', '');
        $followUpStartTime  = $request->get('followUpStartTime', '');
        $followUpEndTime    = $request->get('followUpEndTime', '');
        $preHavingStartTime = $request->get('preHavingStartTime', '');
        $preHavingEndTime   = $request->get('preHavingEndTime', '');
        $sortField          = $request->get('sortField', '');
        $sortOrder          = $request->get('sortOrder', '');
        //$mallRate         = $request->get('mallRate', 0);

        $pageNum  = (int)$request->get('pageNum', 1);
        $pageSize = (int)$request->get('pageSize', 20);

        return $this->success(SiteTaskService::list($pageNum, $pageSize, $rmsUserId, $provinceCode, $cityCode, $districtCode, $wgType, $wgName, $grade, $ranking, $settle, $manageStatus, $projectPhase, $assignId, $assignStartTime, $assignEndTime, $followUpStartTime, $followUpEndTime, $preHavingStartTime, $preHavingEndTime, $sortField, $sortOrder));
    }


    /**
     * 任务详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function detail(Request $request, $id): JsonResponse
    {
        $rmsUserId = getRmsUserId();

        return $this->success(SiteTaskService::detail($id, $rmsUserId));
    }


    /**
     * 指派开发经理
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function assign(Request $request): JsonResponse
    {
        $this->validate($request, [
            'taskIds' => 'required',
            'userId'  => 'required|int'
        ]);

        $rmsUserId = getRmsUserId();
        $taskIdArr = explode(',', $request->post('taskIds'));
        $userId    = (int)$request->post('userId');

        // 跟着权限走
        $power = getRmsUserInfoByAuth();

        if (!in_array('b.openshop.feishu.task.assign', $power->permissions)) {
            return $this->fail(403, '您没有指派开发经理的权限');
        }

        // 指派 or 取消指派
        SiteTaskService::assign($taskIdArr, $userId, $rmsUserId);
        return $this->success();
    }

}
